package com.ruoyi.index.controller;
import com.ruoyi.index.dto.UserDto;
import com.ruoyi.index.service.UserService;
import com.ruoyi.index.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/registerUser")
    public Result addUser(@RequestBody UserDto userDto){
        UserDto user = userService.selectUserName(userDto.getUserName());
        //没有同名用户名
        if(user==null){
            userService.addUser(userDto);
            return Result.success();
        }
        return Result.error("已有用户名");
    }
    @RequestMapping("/loginUser")
    public Result loginUser(@RequestBody UserDto userDto){
        UserDto user = userService.loginUser(userDto);
        if (user!=null&&user.getPassword().equals(userDto.getPassword())){
            return Result.success().put("nickName",user.getNickName());
        }
        return Result.error("登录失败");
    }
}
